$(function()
{
    
    // select ready cue sheet
    $('#cuesheet').one('click', (function() { $(this).select(); }));
    
    // settings
    $('div#settings_link').openCloseBlocks($('div#settings'));
    $('#what_is_separator').dropDownBlock('#explanation_separator', 
        {top: 13, close_on_click: true});
    $('#what_is_file_type').dropDownBlock($('#explanation_file_type'), 
        {top: 13, close_on_click: true});
    
    
    /**
    * Add row
    * 
    */
    $('span.add_row').live('click', function()
    {
        var position = 'after';
        var base = $(this).parents('tr');
        var row = base.clone();
        var callback_func = function() {}
        
        // if we have THEAD row than look for master copy
        if (0 == row.find('td').length) {
            position = 'before';
            base = base.parents('table').find('tr:eq(1)');
            row = base.clone();
            // also put time value '00:00:00.00'
            callback_func = function() 
            {
                row.find('td').each(function() 
                {
                    if ('time' == $(this).attr('class')) {
                        $(this).find('input').val('00');
                    }
                });
            };
        }
        
        // clear row values
        row.find('td').each(function()
        {
            if ('edit' != $(this).attr('class')) {
                $(this).find('input').val('');
            }
        });
        
        base[position](row);
        row.fadeIn('slow', callback_func)
            // remove error class if exists
            .find('td:first').removeClass('error_color').end()
            // focus on prefomer cell
            .find('td:eq(1) input').focus();
        
        fix_numeration();
    });
    
    
    /**
    * Remove row
    * 
    */
    $('span.delete_row').live('click', function()
    {
        var row = $(this).parents('tr');
        
        if ($('#tracklist tr').length > 3) {
            row.remove(); 
            fix_numeration();
        } else {
            alert(default_message_error);
        }
    });
    
    /**
    * Fix table numeration
    * 
    */
    function fix_numeration() 
    {
        
        
        var cells = new Array('perfomer', 'title', 'time');
        $('#tracklist tr').each(function(i)
        {
            // re-numerate rows' numbers
            $(this).find('td.number').text(i);
            
            for (cell=0; cell<cells.length; cell++) {
                $(this).find('td.' + cells[cell] + ' input').each(function() 
                {
                    // re-numerate element's names
                    var name_pattern = /^([a-z]+\[)\d+(\]\[?[a-z]{0,2}\]?)$/;
                    $(this).attr('name', $(this).attr('name')
                        .replace(name_pattern, '$1' + i + '$2'));
                    
                    // re-numerate element's ids
                    var name_pattern = /^([a-z]+-)\d+(-?[a-z]{0,2})$/;
                    $(this).attr('id', $(this).attr('id')
                        .replace(name_pattern, '$1' + i + '$2'));
                });
            }
        });
        
        
    }
    
    
    /**
    * Highlight row under cursor
    * 
    */
    function highlight_row(object) 
    {
        $(object).parent().parent().find('input')
                .css('background-color', '#EDEDF3');
            $(object).css('background-color', '#DFDFEA');
    }
    function clear_highlight_row(object) 
    {
        $(object).parent().parent().find('input')
            .css('background-color', '#FFF');
    }
    
    $.live_bind('#tracklist tr input', 'focus', function() {highlight_row(this);});
    $.live_bind('#common_info tr input', 'focus', function() {highlight_row(this);});
    
    $.live_bind('#tracklist tr input', 'blur', function() {clear_highlight_row(this);});
    $.live_bind('#common_info tr input', 'blur', function() {clear_highlight_row(this);});
    
    
    
    
    
    /**
    * Follow by the arrows
    * 
    */
    $('#common_info td input').keydown(function(e)
    {
        var direction;
        
        switch(e.which) 
        {
            // top
            case 38 : 
                var direction = 'prev';
                break;
            
            // bottom
            case 40 :
                var direction = 'next';
                break;
        }
        if (undefined != direction) {
            $(this).parents('tr')[direction]().find('input').focus().select();
        }
    });
    
    $('#tracklist td input').live('keydown', function(e)
    {
        switch(e.which) 
        {
            // left
            case 37 : 
                var cell = $(this).prev();
                if (0 == cell.length) {
                    var cell = $(this).parent().prev().find('input');
                }
                if (cell.length > 0) {
                    $(this).blur();
                    cell.focus().select();
                }
                break;
            
            // right
            case 39 : 
                var cell = $(this).next();
                
                if (0 == cell.length) {
                    var cells = $(this).parent().next().find('input');
                    var cell = cells.length > 0 ? cell = cells.get(0) : $(this);
                }
                //if (cell.length > 0) {
                    $(this).blur();
                    cell.focus().select();
                //}
                
                break;
            
            // top
            case 38 : 
                var cell_class = $(this).parent().attr('class');
                
                if ('title' == cell_class || 'perfomer' == cell_class) {
                    var cell = $(this).parent().parent().prev()
                        .find('td.' + cell_class + ' input');
                    
                } else {
                    var input_classes = $(this).attr('class').split(' ');
                    var cell = $(this).parent().parent().prev()
                        .find('input.' + input_classes[1]);
                }
                
                if (cell.length > 0) {
                    $(this).blur();
                    cell.focus().select();
                }
                
                break;
            
            // bottom
            case 40 :
                var cell_class = $(this).parent().attr('class');
                
                if ('title' == cell_class || 'perfomer' == cell_class) {
                    var cell = $(this).parent().parent().next()
                        .find('td.' + cell_class).find('input');
                } else {
                    var input_classes = $(this).attr('class').split(' ');
                    var cell = $(this).parent().parent().next()
                        .find('input.' + input_classes[1]);
                }
                
                if (cell.length > 0) {
                    $(this).blur();
                    cell.focus().select();
                }
                
                break;
        }
        
    });
    
    /*function append_global_loader(callback) 
    {
        $('body').prepend('<div id="global_loader_bg"><img \
            src="images/loaders/indicator_blue_large.gif" width="32" \
            height="32" alt="Loading..." id="global_loader" /></div>');
        
        $('#global_loader_bg').css({
            'width':$(document).width(), 
            'height': $(window).height()
        }).fadeIn();
        
        $('#global_loader').show('slow', callback);
    }
    
    function clear_global_loader() 
    {
        $('#global_loader_bg').fadeOut('slow', function()
        {
            $(this).remove();
        });
    }*/
    
    
});